package com.example.carrierapp.ui.splash.guide;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.carrierapp.base.BaseActivity;
import com.example.carrierapp.config.ConstValue;
import com.example.carrierapp.ui.adapter.GuidePageAdapter;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.DisplayUtil;
import com.example.carrierapp.util.SPUtils;
import com.example.lb.fingerdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.ll_guide_port)
    LinearLayout mLlPorts;
    @BindView(R.id.tv_guide_in)
    TextView mTvIn;
    @BindView(R.id.tv_guide_skip)
    TextView mTvSkip;
    @BindView(R.id.guide_page)
    ViewPager mVpView;
    private int[] mImgIds;
    private ImageView[] mIvPointArray;

    @Override
    public int getContentView() {
        return R.layout.activity_guide;
    }

    @Override
    public String getTitleString() {
        return null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initViewPager();
        initPoint();
    }

    /**
     * 加载图片ViewPager
     */
    private void initViewPager() {
        //实例化图片资源
        mImgIds = new int[]{R.mipmap.guide_first, R.mipmap.guide_two, R.mipmap.guide_three};
//        mVpView.init(mImgIds, mLlPorts, mTvIn);
        mVpView.addOnPageChangeListener(this);
        //View集合初始化好后，设置Adapter
        mVpView.setAdapter(new GuidePageAdapter(mImgIds, this));
        //设置滑动监听
    }

    /**
     * 加载底部圆点
     */
    private void initPoint() {
        //根据ViewPager的item数量实例化数组
        mIvPointArray = new ImageView[mImgIds.length];
        //循环新建底部圆点ImageView，将生成的ImageView保存到数组中
        int size = mImgIds.length;
        for (int i = 0; i < size; i++) {
            ImageView iv_point = new ImageView(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(DisplayUtil.dip2px(this, 10), DisplayUtil.dip2px(this, 10));
            lp.setMargins(DisplayUtil.dip2px(this, 8), 0, DisplayUtil.dip2px(this, 8), 0);
            iv_point.setLayoutParams(lp);
            mIvPointArray[i] = iv_point;
            //第一个页面需要设置为选中状态，这里采用两张不同的图片
            if (i == 0) {
                iv_point.setBackgroundResource(R.drawable.shape_guide_select);
            } else {
                iv_point.setBackgroundResource(R.drawable.shape_guide_normal);
            }
            //将数组中的ImageView加入到ViewGroup
            mLlPorts.addView(mIvPointArray[i]);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 过滤按键动作，按返回键不退出程序，直接退到桌面
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);//true对任何Activity都适用
            return true;
        }
        return false;
    }

    @OnClick({R.id.tv_guide_in, R.id.tv_guide_skip})
    public void onGuideInCLick(View v) {
        switch (v.getId()) {
            case R.id.tv_guide_in:
            case R.id.tv_guide_skip:
                SPUtils.putBoolean(this, ConstValue.FIRST_IN, false);
                ActivityUtil.startLogin(this, true);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //循环设置当前页的标记图
        for (int i = 0; i < mImgIds.length; i++) {
            if (position != i) {
                mIvPointArray[i].setBackgroundResource(R.drawable.shape_guide_normal);
            } else {
                mIvPointArray[position].setBackgroundResource(R.drawable.shape_guide_select);
            }
        }
        //判断是否是最后一页，若是则显示按钮
        mTvIn.setVisibility(position == mImgIds.length - 1 ? View.VISIBLE : View.GONE);
        mTvSkip.setVisibility(position == mImgIds.length - 1 ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
